﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataModel;
using SchoolBusiness;

namespace SchoolAdministration
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        private readonly Student blstd = new Student();
        private readonly BLRoutine _blroutine = new BLRoutine();
        private List<MStudent> lstd;
       protected void Page_Load(object sender, EventArgs e)
        {
            lstd = blstd.GetAllStudent();
            if (!IsPostBack)
            {
                DDLSection.Enabled = false;
                LoadComboBoxes();
                LoadStudentGrid(lstd);

            }
        }
        public void LoadComboBoxes()
        {
            List<Grade> grade = _blroutine.GetGrade();
            DDLClass.Items.Clear();
            DDLClass.Items.Insert(0, new ListItem("Class", "0"));
            DDLClass.DataSource = grade;
            DDLClass.DataTextField = "ClassName";
            DDLClass.DataValueField = "ClassId";
            DDLClass.DataBind();
          }
        public void LoadSection(int grade)
        {
            List<MStudent> section = _blroutine.GetSection(grade);
            DDLSection.Items.Clear();
            DDLSection.Items.Insert(0, new ListItem("Section", "0"));
            DDLSection.DataSource = section;
            DDLSection.DataTextField = "Section";
            DDLSection.DataValueField = "Section";
            DDLSection.DataBind();

        }
        public void LoadStudentGrid(List<MStudent> liststd)
        {
            GridViewStd.DataSource = liststd;
            GridViewStd.DataBind();

        }
        public void LoadStudentGrid()
        {
            GridViewStd.DataSource = lstd;
            GridViewStd.DataBind();

        }
        protected void GridViewStd_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("DeleteButton");

                l.Attributes.Add("onclick", "javascript:return " +
                     "confirm('Are you sure you want to delete this record " +
                     DataBinder.Eval(e.Row.DataItem, "Studentid") + "')");

            }
        }
        protected void GridViewStd_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {

        }
        protected void GridViewStd_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //int index = 1;
            //  int subjectId = (int)GridViewSubject.DataKeys[e.RowIndex].Value;
            //DeleteRecordByID(subjectId);
        }



        protected void GridViewStd_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridViewStd.SelectedRow;
        }

        protected void GridViewStd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int studentId = Convert.ToInt32(e.CommandArgument);
                blstd.DeleteStudentbyId(studentId);
                LoadStudentGrid();
            }
        }

        protected void DDLSection_SelectedIndexChanged(object sender, EventArgs e)
        {
            
           // var student=lstd.Where(x => x.Grade==)

        }

        protected void DDLClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLSection.Enabled = true;
            int grade = Int32.Parse(DDLClass.SelectedItem.Value);
            LoadSection(grade);
            int gr=int.Parse(DDLClass.SelectedValue.ToString());
            List<MStudent> student = lstd.Where(x => x.Grade == gr).ToList();
            LoadStudentGrid(student);


        }

          }
}